package com.dems.admin.config;

import com.dems.core.log.TraceIdContext;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

public class RestTemplateTraceIdInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body,
                                        ClientHttpRequestExecution execution) throws IOException {
        String requestId = TraceIdContext.getTraceId();
        request.getHeaders().set(TraceIdContext.TRACE_ID_PREFIX + TraceIdContext.TRACE_ID_KEY, requestId);
        return execution.execute(request, body);
    }
}
